package com.bawei.lib_common.base.logger

import com.bawei.lib_common.base.logger.logger.FileLogger
import com.bawei.lib_common.base.logger.logger.LogCatLogger
import com.bawei.lib_common.base.logger.logger.NetWorkLogger


/**
 *作者：lxy
 *功能:
 *时间：2025/5/22
 */
class Logger private constructor(
    private val TAG: String,
    private val level: LoggerLevel,
    private val loggerStrategy: LoggerStrategy,
    private val isDebug: Boolean,
    private val saveUrl: String
) {
    private var logger: ILogger //logger对象

    init {
        logger = getLogger()
    }

    //次构造必须继承主构造
    constructor(builder: Logger.Builder) : this(
        builder.TAG,
        builder.level,
        builder.loggerStrategy,
        builder.isDebug,
        builder.saveUrl
    )

    /**
     * 策略者模式，根据不同的策略创建不同对象
     */
    private fun getLogger(): ILogger {
        when (loggerStrategy) {
            LoggerStrategy.LOGCAT -> logger = LogCatLogger()
            LoggerStrategy.FILE -> logger = FileLogger()
            LoggerStrategy.NET -> logger = NetWorkLogger()
        }
        logger.setLoggerLevel(level)
        logger.setLogTAG(TAG)
        logger.isDebug(isDebug)
        logger.setSaveUrl(saveUrl)
        return logger
    }

    /**
     * 构建者模式Builder：参数属性比较多，按需设置
     */
    class Builder {
        var TAG: String = "默认TAG"
        var level: LoggerLevel = LoggerLevel.Debug
        var loggerStrategy: LoggerStrategy = LoggerStrategy.LOGCAT
        var isDebug: Boolean = true
        var saveUrl: String = "默认路径"

        fun setTAG(TAG: String): Builder {
            this.TAG = TAG
            return this
        }

        fun setLevel(level: LoggerLevel): Builder {
            this.level = level
            return this
        }

        fun setLoggerStrategy(loggerStrategy: LoggerStrategy): Builder {
            this.loggerStrategy = loggerStrategy
            return this
        }

        fun setDebug(isDebug: Boolean): Builder {
            this.isDebug = isDebug
            return this
        }

        fun setSaveUrl(saveUrl: String): Builder {
            this.saveUrl = saveUrl
            return this
        }

        fun build(): Logger {
            return Logger(this)
        }

    }

    fun d(tag: String, log: String) {
        logger.d(tag, log)
    }

    fun v(tag: String, log: String) {
        logger.v(tag, log)
    }

    fun i(tag: String, log: String) {
        logger.i(tag, log)
    }

    fun w(tag: String, log: String) {
        logger.w(tag, log)
    }

    fun e(tag: String, log: String) {
        logger.e(tag, log)
    }


}